package com.chisheng.caa.service;

import com.chisheng.caa.pojo.ArrangeCourseSetting;
import com.chisheng.caa.pojo.DuplicateStrategy;
import com.chisheng.caa.pojo.dto.ArrangeCourseSettingDTO;
import com.chisheng.caa.pojo.dto.CourseSchedulingDTO;
import com.chisheng.caa.util.BeanUtil;
import lombok.Getter;
import lombok.ToString;
import org.springframework.lang.Nullable;

public abstract class AbstractArrangeCourseService extends AbstractCRUD<CourseSchedulingDTO> implements ArrangeCourseService {

    public AbstractArrangeCourseService(CacheUtil cacheUtil) {
        super(cacheUtil);
    }

    @Override
    public ArrangeCourseSetting getSetting(String accessKey) {
        ArrangeCourseState state = cacheUtil.checkAndGet(accessKey);
        return state.getSetting();
    }

    @Override
    public void setSetting(String accessKey, ArrangeCourseSetting setting) {
        ArrangeCourseState state = cacheUtil.checkAndGet(accessKey);
        state.setSetting(setting);
    }

    @Override
    public DuplicateStrategy getDuplicateStrategy(@Nullable String accessKey) {
        ArrangeCourseState state = cacheUtil.checkAndGet(accessKey);
        return state.getSetting().getDuplicateStrategy();
    }

    @Getter
    @ToString
    public static class ArrangeCourseState extends AbstractState<CourseSchedulingDTO> {

        protected ArrangeCourseSetting setting;

        public void setSetting(ArrangeCourseSetting setting) {
            if (this.setting == null) {
                this.setting = new ArrangeCourseSettingDTO(setting);
            } else {
                BeanUtil.copyProperties(setting, this.setting, true);
            }
        }

    }

}
